package com.lf.freezingpoint.controller;

import com.lf.freezingpoint.common.entity.R;
import com.lf.freezingpoint.entity.SysUser;
import com.lf.freezingpoint.service.LoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.logging.Logger;

@RestController
@Tag(name = "登录/登出",description = "登录/登出接口文档")
@RequestMapping("/api")
public class LoginController {
    private static final Logger logger = Logger.getLogger(LoginController.class.toString());

    @Autowired
    private LoginService loginService;

    @Operation(summary = "登录")
    @PostMapping("/login")
    public R login(@RequestBody SysUser sysUser) {
        if (StringUtils.isBlank(sysUser.getUsername())||StringUtils.isBlank(sysUser.getPassword())){
            return R.fail("用户名或密码错误");
        }
        R r = loginService.login(sysUser);
        logger.info("认证结束");
        return r;
    }

    @Operation(summary = "登出")
    @GetMapping("/logout")
    public R logout(HttpServletRequest request){
        R r = loginService.logout(request);
        logger.info("登出结束");
        return r;
    }
}
